/**
 * 手机号中间4位****隐藏
 * --------------------
 * @param {*} mobile 手机号
 */
const hideMobileInfo = mobile => {
    mobile = "" + mobile;
    var reg = /(\d{3})\d{4}(\d{4})/;
    return mobile.replace(reg, "$1****$2");
}
/**
 * 邮箱号后4位****隐藏
 * --------------------
 * @param {*} email 邮箱账号
 */
const hideEmailInfo = email => {
    if (String(email).indexOf('@') > 0) {
        let newEmail, str = email.split('@'), _s = '';

        if (str[0].length > 4) {
            _s = str[0].substr(0, 4);
            for (let i = 0; i < str[0].length - 4; i++) {
                _s += '*';
            }
        } else {
            _s = str[0].substr(0, 1);
            for (let i = 0; i < str[0].length - 1; i++) {
                _s += '*';
            }
        }
        newEmail = _s + '@' + str[1];
        return newEmail;
    } else {
        return email;
    }
};
/**
 * echart 
 * --------------------
 * @param {*} func 
 * @param {*} wait 
 * @param {*} immediate 
 */
const debounce = (func, wait, immediate) => {
    let timeout, args, context, timestamp, result

    const later = function () {
        // 据上一次触发时间间隔
        const last = +new Date() - timestamp

        // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
        if (last < wait && last > 0) {
            timeout = setTimeout(later, wait - last)
        } else {
            timeout = null
            // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
            if (!immediate) {
                result = func.apply(context, args)
                if (!timeout) context = args = null
            }
        }
    }

    return function (...args) {
        context = this
        timestamp = +new Date()
        const callNow = immediate && !timeout
        // 如果延时不存在，重新设定延时
        if (!timeout) timeout = setTimeout(later, wait)
        if (callNow) {
            result = func.apply(context, args)
            context = args = null
        }

        return result
    }
}
/**
 * @name: 60 秒倒计时
 * @test: test font
 * @msg: 
 * @param {type} 
 * @return: 
 */
const setTime = e => {
    let timer = null
    if (!timer) {
        let TIME_COUNT = 60;
        timer = setInterval(() => {
            if (TIME_COUNT > 0 && TIME_COUNT <= 60) {
                TIME_COUNT--;
                e.target.innerHTML = TIME_COUNT + 's'
                e.target.disabled = 'disabled'
            } else {
                clearInterval(timer);
                e.target.disabled = ''
                e.target.innerHTML = 'Get Code'
                timer = null;
            }
        }, 1000);
    }
}

const resend = element => { 
    let TIME_COUNT = 60;
    var timer = setInterval(function () {    
        TIME_COUNT--
        element.$el.innerText = TIME_COUNT + 's'        
        element.$el.disabled = true
        if (TIME_COUNT === 0) {      
            element.$el.disabled = false    
            element.$el.innerText = 'Get Code' 
            clearInterval(timer)    
        }  
    }, 1000)
}

export {
    hideMobileInfo,
    hideEmailInfo,
    debounce,
    setTime,
    resend
}
